
const Sequelize = require('sequelize');

import sequelize from './db/sequelize';

/**
 * 单张图片实体
 */
const WorkChain= sequelize.define('work_chains', {
  id                    : {type: Sequelize.BIGINT,   allowNull: false, primaryKey: true, autoIncrement: true},
  uid                   : {type: Sequelize.STRING,   allowNull: false},
  
  name                  : {type: Sequelize.STRING,   allowNull: false ,defaultValue : -1},
  type                  : {type: Sequelize.INTEGER,   allowNull: false , defaultValue : -1},
  gategory              : {type: Sequelize.INTEGER,   allowNull: false },

  complete_time         : {type: Sequelize.INTEGER,   allowNull: true },
  complete_place        : {type: Sequelize.STRING,   allowNull: true },

  release_flag          : {type: Sequelize.INTEGER,   allowNull: false , defaultValue : 1},
  release_time          : {type: Sequelize.INTEGER,   allowNull: true },
  release_place         : {type: Sequelize.STRING,   allowNull: true },

  url                   : {type: Sequelize.STRING,   allowNull: false },
  describe              : {type: Sequelize.STRING,   allowNull: false },
  contact_id             : {type: Sequelize.STRING,   allowNull: false },
  hash_code             : {type: Sequelize.STRING,   allowNull: true , unique:'work_hash_index', defaultValue : null },
  handle_code           : {type: Sequelize.STRING,   allowNull: false },

  status                : {type: Sequelize.INTEGER,   allowNull: false , defaultValue : 1 },
  remark                : {type: Sequelize.STRING,   allowNull: true },
  ext                   : {type: Sequelize.TEXT,   allowNull: true },
  ctime                 : {type: Sequelize.INTEGER,   allowNull: true },
  htime                 : {type: Sequelize.INTEGER,   allowNull: true }
},
{
    charset: 'utf8'
});
WorkChain.typeCount = function (params) {
  var sql = 'select count(*) as count , type from work_chains where 1=1'
  if (params && params.uid && params.uid.length >= 1) {
        // if(params.uid.length == 1) {
        //   sql += ' and uid='
        //   sql += `'${params.uid[0]}'`
        // } else {
          var uids = params.uid.join("','")
          sql += " and uid in ('"
          sql += uids
          sql += "')"
        // }
    }
    sql += ' group by type'
    console.log(sql)
   return sequelize.query(sql, { replacements: params, type: Sequelize.QueryTypes.SELECT })
}

WorkChain.gategoryCount = function (params) {
  var sql = 'select count(*) as count , gategory from work_chains where 1=1'
  if (params && params.uid && params.uid.length >= 1) {
          var uids = params.uid.join("','")
          sql += " and uid in ('"
          sql += uids
          sql += "')"
    }
    sql += ' group by gategory'
   return sequelize.query(sql, { replacements: params, type: Sequelize.QueryTypes.SELECT })
}

WorkChain.sync({force: false}).then(function(){});
export default WorkChain;